package com.lt.hr.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.lt.contract.service.UploadExecutor;
import com.lt.hr.entity.THrEmpInfo;
import com.lt.hr.service.THrEmpInfoService;
import com.lt.contract.bean.utils.ResponseResult;
import com.lt.report.util.VeDate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * (THrEmpInfo)表控制层
 *
 * @author makejava
 * @since 2020-03-11 12:12:22
 */
@RestController
@RequestMapping("tHrEmpInfo")
@Slf4j
public class THrEmpInfoController {
    /**
     * 服务对象
     */
    @Resource
    private THrEmpInfoService tHrEmpInfoService;

    @Autowired
    UploadExecutor uploadExecutor;
    /**
     * 获取  移动业务导入列表
     * @param params
     * @return
     */
    @CrossOrigin
    @ResponseBody
    @PostMapping("/queryAll")
    public JSONArray queryAll(@RequestBody String params){
        log.info("\n【请求地址】：{}\n【请求时间】：{}\n", "/queryAll", VeDate.getNowDate());
        List<THrEmpInfo> tHrEmpInfos = null;
        try {
            Map<String,Object> map = JSON.parseObject(params);
            Integer page = (Integer) map.get("page");
            Integer pageSize = (Integer) map.get("pageSize");
            map.put("startSize", page*pageSize-pageSize);
            map.put("endSize", page*pageSize+1);
            tHrEmpInfos = tHrEmpInfoService.queryAll(map);
            JSONArray result = JSON.parseArray(JSON.toJSONString(tHrEmpInfos));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    @CrossOrigin
    @ResponseBody
    @PostMapping("/updateStaff")
    public ResponseResult queryAll(@RequestBody THrEmpInfo params){
        if(params.getEmpPsptId()==null || params.getEmpPsptId().equals("")){
            return new ResponseResult(false,"请传入员工身份证号");
        }
        try {
        log.info("\n【请求地址】：{}\n【请求时间】：{}\n", "/queryAll", VeDate.getNowDate());
        log.info("\n【请求员工】"+params.getLoginId());
            ResponseResult  result = tHrEmpInfoService.update(params);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 获取  移动业务导入列表总数
     * @param params
     * @return
     */
    @CrossOrigin
    @ResponseBody
    @PostMapping("/queryAllCount")
    public Long queryAllCount(@RequestBody String params){
        log.info("\n【请求地址】：{}\n【请求时间】：{}\n", "/queryAllCount", VeDate.getNowDate());
        try {
            Map<String,Object> map = JSON.parseObject(params);
            Long count = tHrEmpInfoService.queryCount(map);
            return count;
        }catch (Exception e){
            e.printStackTrace();
            return 0L;
        }

    }

    @PostMapping(value = "/upload")
    @ResponseBody
    @CrossOrigin
    public String uploadExcel(HttpServletRequest request) {
        log.info("\n【请求地址】：{}\n【请求工号】：{}\n ,【请求时间】：{}\n ", "/upload",request.getHeader("staffId"), VeDate.getNowDate());
        try {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            MultipartFile file = multipartRequest.getFile("file");
            String staffId = request.getHeader("staffId");
            if (file.isEmpty()) {
                return "文件不能为空";
            }
            InputStream inputStream = file.getInputStream();
            log.info("\n【请求地址】：{}\n【请求时间】：{}\n【上传文件名】：{}", "/upload", VeDate.getNowDate(), file.getOriginalFilename());
            String result=tHrEmpInfoService.getBankListByExcel(inputStream, file.getOriginalFilename(),staffId);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return "上传失败。";
        }
    }


}
